var DataTypes = require('sequelize').DataTypes
var _AclPermission = require('./AclPermission')
var _AclRole = require('./AclRole')
var _AclRolePermission = require('./AclRolePermission')
var _AclUser = require('./AclUser')
var _AclUserRole = require('./AclUserRole')
var _CdAnnotation = require('./CdAnnotation')
var _CdDetector = require('./CdDetector')
var _CdSlide = require('./CdSlide')

function initModels(sequelize) {
  var AclPermission = _AclPermission(sequelize, DataTypes)
  var AclRole = _AclRole(sequelize, DataTypes)
  var AclRolePermission = _AclRolePermission(sequelize, DataTypes)
  var AclUser = _AclUser(sequelize, DataTypes)
  var AclUserRole = _AclUserRole(sequelize, DataTypes)
  var CdAnnotation = _CdAnnotation(sequelize, DataTypes)
  var CdDetector = _CdDetector(sequelize, DataTypes)
  var CdSlide = _CdSlide(sequelize, DataTypes)

  AclUser.belongsToMany(AclRole, {
    through: AclUserRole,
    as: 'roles',
    foreignKey: 'user_id',
    otherKey: 'role_id',
  })
  AclRole.belongsToMany(AclUser, {
    through: AclUserRole,
    as: 'users',
    foreignKey: 'role_id',
    otherKey: 'user_id',
  })
  AclUser.hasMany(AclUserRole, {
    as: 'roleIDs',
    foreignKey: 'user_id',
  })
  AclUserRole.belongsTo(AclUser, {
    foreignKey: 'role_id',
  })

  // AclRole.belongsToMany(AclPermission, {
  //   through: AclRolePermission,
  //   foreignKey: 'role_id',
  //   otherKey: 'permission_id',
  // })
  // AclPermission.belongsToMany(AclRole, {
  //   through: AclRolePermission,
  //   foreignKey: 'permission_id',
  //   otherKey: 'role_id',
  // })

  AclRole.hasMany(AclRolePermission, {
    foreignKey: 'role_id',
    as: 'perms',
  })
  AclPermission.hasMany(AclRolePermission, {
    foreignKey: 'permission_id',
    as: 'roles',
  })
  AclRolePermission.belongsTo(AclRole, {
    foreignKey: 'role_id',
  })
  AclRolePermission.belongsTo(AclPermission, {
    as: 'perm',
    foreignKey: 'permission_id',
  })

  CdSlide.hasMany(CdDetector, {
    as: 'dets',
    foreignKey: 'slide_id',
  })
  CdDetector.belongsTo(CdSlide, {
    foreignKey: 'slide_id',
  })

  CdDetector.hasMany(CdAnnotation, {
    foreignKey: 'slide_id',
  })
  CdAnnotation.belongsTo(CdDetector, {})

  return {
    AclPermission,
    AclRole,
    AclRolePermission,
    AclUser,
    AclUserRole,
    CdAnnotation,
    CdDetector,
    CdSlide,
  }
}
module.exports = initModels
